package com.lanhai.longheng.utils;
 
import com.lanhai.longheng.beans.AgvMountBean; 

public class Tools {
	public static String byte2HexStr(byte[] b) {
		String hs = "";
		String stmp = "";
		for (int n = 0; n < b.length; n++) {
			stmp = (Integer.toHexString(b[n] & 0XFF));
			if (stmp.length() == 1)
				hs += " 0" + stmp;
			else
				hs += " " + stmp;  
		}
		return hs.toUpperCase().substring(1);
	}
	
	public static short toIntL(byte[] b) {
		short res = 0;
		for (int i = 0; i < b.length; i++) {
			res += (b[i] & 0xff) << (i * 8);
		}
		return res;
	}
	
	public static byte[] toLH(int n) {  
		  byte[] b = new byte[4];  
		  b[0] = (byte) (n & 0xff);  
		  b[1] = (byte) (n >> 8 & 0xff);  
		  b[2] = (byte) (n >> 16 & 0xff);  
		  b[3] = (byte) (n >> 24 & 0xff);  
		  return b;  
		}
	
	
	public static short toIntH(byte[] b){
		short res = 0;
	    for(int i=0;i<b.length;i++){
	        res += (b[i] & 0xff) << ((3-i)*8);
	    }
	    return res;
	}
	
	public static long strToLong(String s) {
		try {
			return Long.parseLong(s);
		}
		catch(Exception e) {
			return 0;
		}
	}
	
	public static int strToInt(String s) {
		try {
			return Integer.parseInt(s);
		}
		catch(Exception e) {
			return 0;
		}
	}
	
	/**
	 * 计算agv离开交管的距离
	 * @param agvx
	 * @param agvy
	 * @param areax
	 * @param areay
	 * @return
	 */
	public static boolean farawayArea(int agvx,int agvy,int areax,int areay) {
		int dix=agvx-areax;
		int diy=agvy-areay;
		double dist=Math.sqrt(dix*dix+diy*diy);
		if(dist>=200) { //200cm=2米
			return true;
		}else {
			return false;
		}
	}
	
	public static String getProcess(String nextProcess) {
		if(nextProcess==null||nextProcess.equals("")) return "";
		if(nextProcess.contains("制绒上")) {
			return "制绒上料";
		}else 	if(nextProcess.contains("制绒下")) {
			return "制绒下料";
		}else 	if(nextProcess.contains("SE")) {
			return "SE";
		}else 	if(nextProcess.contains("前氧")) {
			return "前氧";
		}else 	if(nextProcess.contains("PSG")) {
			return "PSG";
		}else if(nextProcess.contains("扩散")) {
			return "扩散";
		}
		return "";
	}
	 
	/**
	 * 二维码能离开的条件
	 * @param process
	 * @param agvmount
	 * @return
	 */
	public static boolean agvCanLeave(String process,AgvMountBean agvmount) {
		if(process==null||process.equals("")) return false;
		if(process.contains("制绒上")) {
			// 只要下面空，就能去，交给201去处理
			if(agvmount.isDownFull()==false) {
				return true;
			} 
		}else 	if(process.contains("制绒下")) {
			//上空下空才能去
			if(agvmount.isUpFull()==false  && agvmount.isDownFull()==false) {
				return true;
			} 
		}else if(process.contains("扩散")) {
			//上满下空才能去
			if(agvmount.isUpFull()  && agvmount.isDownFull() ==false) {
				return true;
			} 
		}else 	if(process.contains("SE")) {
			//上空下满才能去
			if(agvmount.isUpFull() ==false  && agvmount.isDownFull() ) {
				return true;
			} 
		}else 	if(process.contains("前氧")) {
			//上满下空才能去
			if(agvmount.isUpFull()  && agvmount.isDownFull() ==false) {
				return true;
			} 
		}else 	if(process.contains("PSG")) {
			//上空下满,或者全空 才能去
			if(agvmount.isUpFull()==false  && agvmount.isDownFull() ) {
				return true;
			} 
		}
		return false;
	}
	
	/**
	 * 传输位能传输的条件
	 * @param process
	 * @param agvmount
	 * @return
	 */
	public static boolean agvCanTrans(String process,AgvMountBean agvmount) {
		if(process==null||process.equals("")) return false;
		if(process.contains("制绒上")) {
			//AGV上面有料才能传
			 if(agvmount.isUpFull()) {
				 return true;
			 }
		}else 	if(process.contains("制绒下")) {
			//AGV上空就可传
			 if(agvmount.isUpFull()==false) {
				 return true;
			 }
		}else if(process.contains("扩散")) {
			//AGV上满下空才能传
			if(agvmount.isUpFull()  && agvmount.isDownFull()==false ) {
				return true;
			} 
		}else 	if(process.contains("SE")) {
			 //AGV下满上空才能传
			if(agvmount.isUpFull()==false  && agvmount.isDownFull() ) {
				return true;
			} 
		}else 	if(process.contains("前氧")) {
			//AGV上满下空才能传
			if(agvmount.isUpFull()  && agvmount.isDownFull()==false ) {
				return true;
			} 
		}else 	if(process.contains("PSG")) {
			//AGV下满上空才能传
			if(agvmount.isUpFull()==false  && agvmount.isDownFull() ) {
				return true;
			} 
		}
		return  false;
		
	}
}
